define(function(require, exports) {
	var pubjs = require('pubjs');
	var util = require('util');
	var view  = require('@base/view');
	var $ = require('jquery');

	// 行业分类渲染函数
	function _renderCategories(sels, dom){
		var html = [];
		if(!util.isArray(sels))
		{
			return sels.Name;
		}

		util.each(sels, function(s) {
			html.push(s.Name);
		});

		return html.join('-');
	}

	var group = pubjs.config('exchange_group');
	var cache = {};

	var map = {
		industry: {
			10024: 10009
			,10026: 10016
			,10033: 10044
		}
	};

	// 活动编辑，单独出价广告位
	var Industry = view.container.extend({
		init: function(config)
		{
			config = pubjs.conf(config, {
				'url': 'category/industry',
				dpConfig: {
					'option_height': 300
					,'reqType': 'websocket'
					,'auto_load': false
					,'key': 'BdxId'
					,'name': 'Name'
					,'skey': 'children'
					,'def': LANG('选择行业分类')
					,'render': _renderCategories
					,'canSelectParent': false
					,'isPopUp': false
					,'width': 300
				}
				,view_model: {
					hasTwoLevel: false // 是不是只有一层
				}
				,type: 'industry'
			});

			this.$hasTwoLevel = false;
			this.$dataReady = false;
			this.$param = {};
			this.Super('init', arguments);
		}
		,afterBuild: function()
		{
			this.append([
				'<div ms-class="dn: hasTwoLevel" class="M-formSectionItem" pub-name="one" pub-mod="@base/common/dropdown.base"></div>',
				'<div ms-class="dn: !hasTwoLevel" class="M-formSectionItem" pub-name="two" pub-mod="@base/common/dropdown.subDropdown"></div>'
			].join(''));

			var c = this.getConfig('dpConfig');
			this.buildFromTemplate({
				one: c,
				two: c
			});
		}
		,reset: function()
		{
			this.$hasTwoLevel = false;
			this.$dataReady = false;
			this.$.one.setData(null, []);
			this.$.one.reset();
			this.$.two.setData(null, []);
			this.$.two.reset();
			this.vm.reset();
			return this;
		}
		,setParam: function(param)
		{
			if(param.ChannelId)
			{
				var c = this.getConfig();
				var cid = param.ChannelId;
				if(map[c.type] && map[c.type][cid])
				{
					param.ChannelId = map[c.type][cid];
				}
			}

			$.extend(this.$param, param);
			return this;
		}
		,load: function()
		{
			var self = this;
			var c = self.getConfig();
			var cid = self.$param && self.$param.ChannelId;
			this.$cid = +cid;

			var key = c.type + '_' + cid;
			if(cid && cache[key])
			{
				self.setData(cache[key]);
			}
			else
			{
				if(self.$mid)
				{
					pubjs.mc.abort(self.$mid, true);
				}

				self.$mid = pubjs.mc.send(c.url, self.$param, function(err, data) {
					self.$mid = 0;
					if(err)
					{
						return pubjs.alert(err && err.message || LANG('拉取数据错误'));
					}

					cache[key] = data && data.items || [];
					self.setData(cache[key]);
				})
			}
		}
		,setValue: function(data, def)
		{
			if((!data || !data.length) && !def)
			{
				return this;
			}

			var self = this;
			var adClass = data && data[0] || def;
			var cid = self.$cid;
			var value;
			switch (true)
			{
				case util.exist(group.appFlood, cid):
					// 为了处理没有下级的情况
					if((Math.floor(adClass/100)) === 0)
					{
						value = [adClass];
					}
					else
					{
						value = [Math.floor(adClass/100), adClass];
					}
					break;
				case util.exist(group.huzhong, cid):
					// 如果是三位数，则转为四位数
					if(String(adClass).length < 4)
					{
						adClass = '0'+ adClass;
					}

					value = [String(adClass).substr(0,2), adClass];
					break;
				case util.exist(group.valueMaker, cid):
				case util.exist(group.baidu_ad, cid):
					value = [String(adClass).substr(0,2), adClass];
					break;
				case cid == 10097:
				case util.exist(group.chuanyang, cid):
					value = [String(adClass).substr(0,3), adClass];
					break;
				case util.exist(group.mopub, cid):
				case util.exist(group.juxiao, cid):
				case util.exist(group.yincheng, cid):
					value = data;
					break;
				default:
					value = adClass;
			}

			this.$value = value;
			// 再设置一次值
			var dp = self.$hasTwoLevel ? self.$.two : self.$.one;
			if(dp && self.$dataReady){
				dp.setData(self.$value);
			}
			return this;
		}
		,getValue: function()
		{
			var self = this;
			var exchange_id = self.$cid;
			var dp = self.$hasTwoLevel ? self.$.two : self.$.one;
			var adClass = dp.getData();
			if(!adClass)
			{
				return [];
			}

			switch (true)
			{
				case util.exist(group.appFlood, exchange_id):
				case util.exist(group.valueMaker, exchange_id):
				case util.exist(group.huzhong, exchange_id):
				case util.exist(group.chuanyang, exchange_id):
				case util.exist(group.mongo, exchange_id):
				case util.exist(group.baidu_ad, exchange_id):
				case exchange_id == 10097:
					if(adClass && util.isArray(adClass))
					{
						adClass = adClass && adClass.splice(-1, 1) || 0;
					}
					break;
				default :
					break;

			}

			this.$value = adClass;
			return adClass;
		}
		,setData: function(data)
		{
			this.$hasTwoLevel = false;
			this.vm.$.hasTwoLevel = false;
			if(data && data[0] && data[0].children && data[0].children.length)
			{
				this.$hasTwoLevel = true;
				this.vm.$.hasTwoLevel = true;
			}

			if(this.$hasTwoLevel)
			{
				this.$.two.setData(this.$value, data);
			}
			else
			{
				var v = this.$value;
				if(util.isArray(this.$value))
				{
					v = v[0];
				}

				this.$.one.setData(v, data);
			}
			this.$dataReady = true;
			return this;
		}
	});

	exports.industry = Industry;

	exports.sensitive = Industry.extend({
		init: function(config)
		{
			config = pubjs.conf(config, {
				'url': 'category/sensitive',
				dpConfig: {
					'option_height': 300
					,'reqType': 'websocket'
					,'auto_load': false
					,'key': 'BdxId'
					,'name': 'Name'
					,'skey': 'children'
					,'def': LANG('选择敏感分类')
					,'render': _renderCategories
					,'canSelectParent': false
					,'isPopUp': false
					,'width': 300
				}
				,view_model: {
					hasTwoLevel: false // 是不是只有一层
				}
				,type: 'sensitive'
			});

			this.Super('init', arguments);
		}
		,setValue: function(data, def)
		{
			if((!data || !data.length) && !def)
			{
				return this;
			}

			this.$value = data;
			return this;
		}
		,getValue: function()
		{
			var self = this;
			var dp = self.$hasTwoLevel ? self.$.two : self.$.one;
			var adClass = dp.getData();
			if(!adClass)
			{
				return [];
			}

			this.$value = adClass;
			return adClass;
		}

	});
});